<template>
  <div>
    <el-dialog
      center
      :title="info.isadd ? '添加角色' : '编辑角色'"
      :visible.sync="info.isshow"
      :before-close="cancel"
    >
      <el-form :model="rolerForm" :rules="rules" ref="roler">
        <el-form-item
          label="角色名称"
          prop="rolename"
          :label-width="formLabelWidth"
        >
          <el-input v-model="rolerForm.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" :label-width="formLabelWidth">
          <el-tree
            ref="tree"
            :data="menulist"
            show-checkbox
            node-key="id"
            :props="{
              children: 'children',
              label: 'title',
            }"
            default-expand-all
            check-strictly
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="rolerForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="addInfo" v-if="info.isadd" type="primary"
          >添 加</el-button
        >
        <el-button @click="editInfo" v-else type="primary">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入辅助性函数
import { mapActions, mapGetters } from "vuex";
//引入封装好的添加接口
import { roleAdd, roleEdit, roleInfo } from "../../util/request/api";
export default {
  //通过props去接收父组件的数据
  props: ["info"],
  data() {
    return {
      formLabelWidth: "120px",
      rolerForm: {
        rolename: "",
        menus: "",
        status: 1,
      },
      //设定规则验证
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenuList",
      Rolelist: "role/getRoleList",
    }),
  },
  mounted() {
    //调用菜单行动
    this.getMenuList();
    this.getRoleList()
  },
  methods: {
    ...mapActions({
      getMenuList: "menu/getMenuAction",
      getRoleList: "role/getRoleAction",
    }),
    // 封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      //关闭弹框的时候调用清空弹框
      this.empty();
    },
    //封装一个清空函数
    empty() {
      this.rolerForm = {
        rolename: "",
        menus: "",
        status: 1,
      };
      //单独清空属性结构
      this.$refs.tree.setCheckedKeys([]);
    },
    // 封装一个添加事件
    addInfo() {
      this.$refs.roler.validate((valid) => {
        if (valid) {
          console.log(this.$refs.tree.getCheckedKeys(), "获取你选中的key");
          /* 
          后端接口菜单权限是要求用字符串接收，字符串必须是','分割
          */
          this.rolerForm.menus = this.$refs.tree.getCheckedKeys().join(",");
          //调取添加接口
          roleAdd(this.rolerForm).then((res) => {
            console.log(res, "角色的添加");
            if (res.code == 200) {
              this.$message.success(res.msg);
              //添加成功之后要关闭弹框
              this.cancel();
              //重新调用一下列表接口
              this.getRoleList();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log(valid, "err");
        }
      });
    },
    // 封装一个查询单条数据事件
    lookup(id) {
      // console.log(id,'每一条数据的id');
      //获取一条数据
      roleInfo({ id }).then((res) => {
        if (res.code == 200) {
          this.rolerForm = res.list;
          this.rolerForm.id = id;
          //数据回显给数结构设置值
          this.$refs.tree.setCheckedKeys(this.rolerForm.menus.split(","));
        }
      });
    },
    // 封装一个编辑事件
    editInfo() {
      this.$refs.roler.validate((valid) => {
        if (valid) {
          /* 
          后端接口菜单权限是要求用字符串接收，字符串必须是','分割
          */
          this.rolerForm.menus = this.$refs.tree.getCheckedKeys().join(",");
          //调取添加接口
          roleEdit(this.rolerForm).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              //添加成功之后要关闭弹框
              this.cancel();
              //重新调用一下列表接口
              this.getRoleList();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log(valid, "err");
        }
      });
    },
  },
};
</script>

<style scoped></style>
